package com.pengshun.modules.api.coal.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pengshun.modules.api.system.domain.Citys;
import com.pengshun.modules.api.system.domain.Districts;
import com.pengshun.modules.api.system.domain.Provinces;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class CommonUtil {

    public static String getFourRandom() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Random random = new Random();
        String fourRandom = random.nextInt(10000) + "";
        int randLength = fourRandom.length();
        if (randLength < 4) {
            for (int i = 1; i <= 4 - randLength; i++) {
                fourRandom = "0" + fourRandom;
            }

        }
        String no=format.format(new Date())+fourRandom;
        return no;
    }

    private static double EARTH_RADIUS = 6371.393;

    public static double GetDistance(Double lat1, Double lng1, Double lat2, Double lng2) {
        if(lat1==null||lat2==null||lng1==null||lng2==null){
            return 0;   
        }
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s);
        return s;
    }

    private static double rad(double d) {
        return  d * Math.PI / 180.0;
    }

    /**
     * 获取高德所有省市区资料
     */
    private static  void getAllCityByGaode(){
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://restapi.amap.com/v3/config/district?subdistrict=4&key=f89f8193e74144946e5a581295527882&extensions=base";
        ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
        String body = forEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONArray list = (JSONArray) jsonObject.get("districts");
        List<Provinces> provinceList = new ArrayList<>();
        List<Citys> cityList = new ArrayList<>();
        List<Districts> districtList = new ArrayList<>();
        for (Object o : list) {
            JSONArray ooo = (JSONArray) ((JSONObject) o).get("districts");
            for (Object o1 : ooo) {
                Integer id = Integer.parseInt(((JSONObject) o1).get("adcode").toString());
                Provinces province = new Provinces(id, ((JSONObject) o1).get("name").toString());
                provinceList.add(province);
                JSONArray citys = (JSONArray) ((JSONObject) o1).get("districts");
                for (Object cit : citys) {
                    Integer cityId = Integer.parseInt(((JSONObject) cit).get("adcode").toString());
                    Citys city = new Citys(cityId, ((JSONObject) cit).get("name").toString(), id);
                    cityList.add(city);
                    JSONArray dd = (JSONArray) ((JSONObject) cit).get("districts");
                    for (Object o2 : dd) {
                        Integer did = Integer.parseInt( ((JSONObject) o2).get("adcode").toString());
                        Districts district = new Districts(did, ((JSONObject) o2).get("name").toString(), cityId);
                        districtList.add(district);
                    }

                }

            }
        }
    }
}
